package selcoursesys.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import selcoursesys.dao.CourseDAO;
import selcoursesys.dao.SelectionDAO;
import selcoursesys.dao.StudentDAO;
import selcoursesys.dao.impl.CourseDAOImpo;
import selcoursesys.dao.impl.SelectionDAOImpl;
import selcoursesys.dao.impl.StudentDAOImpl;
import selcoursesys.entity.*;
import selcoursesys.service.CourseService;
import selcoursesys.service.SearchService;
import selcoursesys.service.SelectionService;
import selcoursesys.service.impl.CourseServiceImpl;
import selcoursesys.service.impl.GetMajorAb;
import selcoursesys.service.impl.SearchServiceImpl;
import selcoursesys.service.impl.SelectionServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by Lpbdz on 2017/4/11.
 */
public class SearchInfoAction extends ActionSupport{
    public String searchCourse() throws Exception{
        HttpServletRequest request = ServletActionContext.getRequest();
        String keyword = request.getParameter("keyword");
        String stuid=request.getParameter("stuid");
        String term=request.getParameter("term");
        List<CourseInfo> courseInfos=new LinkedList<CourseInfo>();
        StudentDAO studentDAO=new StudentDAOImpl();
        String majorId=studentDAO.queryStudentMajor(stuid);
        //System.out.println("学生专业"+majorId);
        //根据专业号GET专业缩写
        String majorAb= GetMajorAb.getMajorAb(majorId);
        //System.out.println(majorAb);
        //根据专业缩写找到必修课，选修课，辅修课，和所有课程
        CourseDAO courseDAO=new CourseDAOImpo();
        SearchService searchService=new SearchServiceImpl();
        List<CourseEntity> listSearch=searchService.searchInfo(majorAb,keyword,term);
        Iterator iterator=listSearch.iterator();
        while(iterator.hasNext()){
            CourseEntity courseEntity=(CourseEntity)iterator.next();
            System.out.println(courseEntity.getCourseid()+" "+courseEntity.getCoursename()+" "+courseEntity.getCoursetype()+" "+courseEntity.getTeacherEntity().getTname()+" "+courseEntity.getCredit());
            courseInfos.add(new CourseInfo(stuid,courseEntity.getCourseid(),courseEntity.getCoursename(),courseEntity.getCredit(),courseEntity.getTeacherEntity().getTname(),courseEntity.getCoursetype()));
        }
        ActionContext.getContext().put("searchresult",courseInfos);
        return "go_to_res";
    }

    //教师查找学生信息
    public String searchStu() throws Exception{
        HttpServletRequest request = ServletActionContext.getRequest();
        //String keyword = "计科";
        //String teaid="1996001001";
        List<StudentInfo> studentInfos=new LinkedList<StudentInfo>();
        String keyword=request.getParameter("keyword");
        String s1=new String(keyword.getBytes("ISO-8859-1"),"utf-8");
        System.out.println("s1"+s1);
        String teaid=request.getParameter("teaid");
        //根据教师id GET course
        keyword=s1;
        System.out.println(teaid+" "+keyword);
        CourseDAO courseDAO=new CourseDAOImpo();
        CourseEntity courseEntity=courseDAO.queryCourseTid(teaid);
        //根据课程id查看选课学生
        SelectionDAO selectionDAO=new SelectionDAOImpl();
        List<SelectionEntity> selectionEntities=selectionDAO.querySelectCid(courseEntity.getCourseid());

        Iterator iterator=selectionEntities.iterator();
        while(iterator.hasNext()){
            SelectionEntity selectionEntity=(SelectionEntity)iterator.next();
            //根据学号get学生信息
            System.out.println("学号"+selectionEntity.getStuid());
            StudentDAO studentDAO=new StudentDAOImpl();
            List<StudentEntity> studentEntities=studentDAO.queryStudentId(selectionEntity.getStuid());
            Iterator iterator1=studentEntities.iterator();
            while(iterator1.hasNext()){
                StudentEntity studentEntity=(StudentEntity)iterator1.next();
                String info="";
                info=info+studentEntity.getStuid()+","+studentEntity.getStuname()+","+studentEntity.getAcademyByAid().getAname()+","+studentEntity.getClazzByClassid().getClassname();
                System.out.println(info);
                if(info.indexOf(keyword)!=-1){
                    //System.out.println(info);
                    //System.out.println("hello");
                    studentInfos.add(new StudentInfo(studentEntity.getStuid(),studentEntity.getStuname() ,studentEntity.getAcademyByAid().getAname(),studentEntity.getClazzByClassid().getClassname()));
                }
            }

        }
        ActionContext.getContext().put("stuinfo",studentInfos);
        return "go_to_resstu";
    }
}
